package com.example.myapplication.ui.dashboard;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentDashboardBinding;
import com.example.myapplication.ui.home.AdsAdapter;
import com.example.myapplication.ui.home.entity.Data;
import com.example.myapplication.ui.study.StudyFragment;

import java.util.ArrayList;
import java.util.List;

public class DashboardFragment extends Fragment {

    private FragmentDashboardBinding binding;

    private RecyclerView recyclerView;
    private DashAdsAdapter adsAdapter;
    private List<Data> adsList;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textDashboard;
        dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);

        final RecyclerView recyclerViewAds = binding.recyclerViewAds;
        recyclerView = recyclerViewAds.findViewById(R.id.recycler_viewAds);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        adsList = new ArrayList<>();
        // 填充广告数据
        adsList.add(new Data(1L,"第一章 职业理念","职业理念",""));
        adsList.add(new Data(2L,"第二章 职业道德","职业道德",""));
        adsList.add(new Data(3L,"第三章 教育法律和法规","教育法律和法规",""));
        adsList.add(new Data(4L,"第四章 基本能力","基本能力",""));
        // ...添加更多广告URL



        adsAdapter = new DashAdsAdapter(adsList);
        adsAdapter.setOnItemClickListener(new DashAdsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Data data, int position) {

                Fragment secondFragment = new StudyFragment();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragmentContainer, secondFragment);
                fragmentTransaction.setReorderingAllowed(true); // 允许重新排序
                fragmentTransaction.addToBackStack(null); // 或者使用特定的名称
                fragmentTransaction.commit();
            }
        });

        recyclerView.setAdapter(adsAdapter);
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}